<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="在上篇在讲解RegistryDirectory的时候，dubbo管理员可以通过dubbo-admin管理系统在线上修改dubbo服务提供者的参数，最终将存储在注册中心的configurators catalog，然后通知RegistryDirectory更新服务提供者的URL中相关属性，按照最新的配置，重新创建Invoker并销毁原来的Invoker。有关官方文档关于动态改变配置(override">
<meta property="og:type" content="article">
<meta property="og:title" content="源码分析Dubbo配置规则机制(override协议)">
<meta property="og:url" content="https://www.codingw.net/posts/a0fe1016.html">
<meta property="og:site_name" content="中间件兴趣圈">
<meta property="og:description" content="在上篇在讲解RegistryDirectory的时候，dubbo管理员可以通过dubbo-admin管理系统在线上修改dubbo服务提供者的参数，最终将存储在注册中心的configurators catalog，然后通知RegistryDirectory更新服务提供者的URL中相关属性，按照最新的配置，重新创建Invoker并销毁原来的Invoker。有关官方文档关于动态改变配置(override">
<meta property="og:locale">
<meta property="og:image" content="https://img-blog.csdn.net/20180624225822489?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">
<meta property="og:image" content="https://img-blog.csdn.net/2018062422584447?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">
<meta property="og:image" content="https://img-blog.csdn.net/20180624225915334?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">
<meta property="og:image" content="https://img-blog.csdn.net/20180624225933538?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">
<meta property="og:image" content="https://img-blog.csdn.net/20180624230247836?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">
<meta property="article:published_time" content="2020-11-01T15:14:01.000Z">
<meta property="article:modified_time" content="2021-04-26T12:10:15.371Z">
<meta property="article:author" content="中间件兴趣圈">
<meta property="article:tag" content="中间件">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdn.net/20180624225822489?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://www.codingw.net/posts/a0fe1016.html"/>





  <title>源码分析Dubbo配置规则机制(override协议) | 中间件兴趣圈</title>
  








<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">中间件兴趣圈</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">微信搜『中间件兴趣圈』，回复『Java』获取200本优质电子书</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://www.codingw.net/posts/a0fe1016.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="中间件兴趣圈">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">源码分析Dubbo配置规则机制(override协议)</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-11-01T23:14:01+08:00">
                2020-11-01
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/dubbo/" itemprop="url" rel="index">
                    <span itemprop="name">dubbo</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/posts/a0fe1016.html" class="leancloud_visitors" data-flag-title="源码分析Dubbo配置规则机制(override协议)">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>次
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <div id="vip-container"><p>在上篇在讲解RegistryDirectory的时候，dubbo管理员可以通过dubbo-admin管理系统在线上修改dubbo服务提供者的参数，最终将存储在注册中心的configurators catalog，然后通知RegistryDirectory更新服务提供者的URL中相关属性，按照最新的配置，重新创建Invoker并销毁原来的Invoker。<br>有关官方文档关于动态改变配置(override协议）的详细描述如下：<br><img src="https://img-blog.csdn.net/20180624225822489?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述"><br><img src="https://img-blog.csdn.net/2018062422584447?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述"><br>dubbo-admin 管理后台，界面如下：<br><img src="https://img-blog.csdn.net/20180624225915334?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述"><br><img src="https://img-blog.csdn.net/20180624225933538?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述"></p>
<span id="more"></span>

<p>当Dubbo管理人员在上述界面，选择配置后点击保存，会构建override:// url存入到注册中心(configurators) catalog下，此时基于注册中心发现服务提供者的监听器(RegistryDirectory)会收到回调(notify)方法，接下来我们再来看一下RegistryDirectory#notify方法。</p>
<p>RegistryDirectory#notify</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">public synchronized void notify(List&lt;URL&gt; urls) &#123;        &#x2F;&#x2F; @1</span><br><span class="line">        List&lt;URL&gt; invokerUrls &#x3D; new ArrayList&lt;URL&gt;();</span><br><span class="line">        List&lt;URL&gt; routerUrls &#x3D; new ArrayList&lt;URL&gt;();</span><br><span class="line">        List&lt;URL&gt; configuratorUrls &#x3D; new ArrayList&lt;URL&gt;();</span><br><span class="line">        for (URL url : urls) &#123;</span><br><span class="line">            String protocol &#x3D; url.getProtocol();</span><br><span class="line">            String category &#x3D; url.getParameter(Constants.CATEGORY_KEY, Constants.DEFAULT_CATEGORY);</span><br><span class="line">            if (Constants.ROUTERS_CATEGORY.equals(category) </span><br><span class="line">                    || Constants.ROUTE_PROTOCOL.equals(protocol)) &#123;</span><br><span class="line">                routerUrls.add(url);</span><br><span class="line">            &#125; else if (Constants.CONFIGURATORS_CATEGORY.equals(category) </span><br><span class="line">                    || Constants.OVERRIDE_PROTOCOL.equals(protocol)) &#123;</span><br><span class="line">                configuratorUrls.add(url);</span><br><span class="line">            &#125; else if (Constants.PROVIDERS_CATEGORY.equals(category)) &#123;</span><br><span class="line">                invokerUrls.add(url);</span><br><span class="line">            &#125; else &#123;</span><br><span class="line">                logger.warn(&quot;Unsupported category &quot; + category + &quot; in notified url: &quot; + url + &quot; from registry &quot; + getUrl().getAddress() + &quot; to consumer &quot; + </span><br><span class="line">                     NetUtils.getLocalHost());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        &#x2F;&#x2F; configurators </span><br><span class="line">        if (configuratorUrls !&#x3D; null &amp;&amp; configuratorUrls.size() &gt;0 )&#123;       &#x2F;&#x2F; @2</span><br><span class="line">            this.configurators &#x3D; toConfigurators(configuratorUrls);</span><br><span class="line">        &#125;</span><br><span class="line">        &#x2F;&#x2F; routers</span><br><span class="line">        if (routerUrls !&#x3D; null &amp;&amp; routerUrls.size() &gt;0 )&#123;</span><br><span class="line">            List&lt;Router&gt; routers &#x3D; toRouters(routerUrls);</span><br><span class="line">            if(routers !&#x3D; null)&#123; &#x2F;&#x2F; null - do nothing</span><br><span class="line">                setRouters(routers);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        List&lt;Configurator&gt; localConfigurators &#x3D; this.configurators; &#x2F;&#x2F; local reference</span><br><span class="line">        &#x2F;&#x2F; 合并override参数</span><br><span class="line">        this.overrideDirectoryUrl &#x3D; directoryUrl;</span><br><span class="line">        if (localConfigurators !&#x3D; null &amp;&amp; localConfigurators.size() &gt; 0) &#123;</span><br><span class="line">            for (Configurator configurator : localConfigurators) &#123;</span><br><span class="line">                this.overrideDirectoryUrl &#x3D; configurator.configure(overrideDirectoryUrl);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        &#x2F;&#x2F; providers</span><br><span class="line">        refreshInvoker(invokerUrls);    &#x2F;&#x2F; @3</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>由于这个方法的实现在上一篇文章<a target="_blank" rel="noopener" href="https://blog.csdn.net/prestigeding/article/details/80727275">《源码分析Dubbo服务发现机制(RegistryDirectory)》</a>中详细分析，故这里只列出与本文章相关的关注点：<br>代码@1：参数为当前configurators目录下所有的URL,例如：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">urls: [override:&#x2F;&#x2F;0.0.0.0&#x2F;com.wuys.frame.api.service.IUserService?category&#x3D;configurators&amp;dynamic&#x3D;false&amp;enabled&#x3D;true&amp;timeout&#x3D;10000, override:&#x2F;&#x2F;0.0.0.0&#x2F;com.wuys.frame.api.service.IUserService?category&#x3D;configurators&amp;dynamic&#x3D;false&amp;enabled&#x3D;true&amp;weight&#x3D;200]。</span><br></pre></td></tr></table></figure>
<p>代码@2:将override url转换为List&lt; Configurator&gt;，是本节重点要讨论的内容。<br>代码@3：调用refreshInvoker方法，由于这里的invokerUrls为空，此时会对原先的invoker马上应用新的配置参数吗？带着这个疑问，我们先看一下refreshInvoker是如何处理的，然后回头重点分析代码@2的实现细节。<br>关于refreshInvoker的实现，在上一篇<a target="_blank" rel="noopener" href="https://blog.csdn.net/prestigeding/article/details/80727275">源码分析Dubbo服务注册与发现机制RegistryDirectory)</a>中也详细分析过，这里只是为了求证一下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">if (invokerUrls.size() &#x3D;&#x3D; 0 &amp;&amp; this.cachedInvokerUrls !&#x3D; null)&#123;</span><br><span class="line">       invokerUrls.addAll(this.cachedInvokerUrls);</span><br><span class="line"> &#125; else &#123;</span><br><span class="line">      this.cachedInvokerUrls &#x3D; new HashSet&lt;URL&gt;();</span><br><span class="line">            this.cachedInvokerUrls.addAll(invokerUrls);&#x2F;&#x2F;缓存invokerUrls列表，便于交叉对比</span><br><span class="line">      &#125;</span><br><span class="line">     if (invokerUrls.size() &#x3D;&#x3D;0 )&#123;</span><br><span class="line">          return;</span><br><span class="line">     &#125;</span><br><span class="line">   &#x2F;&#x2F; 省略部分代码</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>从这里看出，如果invokerUrls为空，如果已缓存的服务提供者不为空，则将已缓存的服务提供者加入到invokerUrls中，此时invokerUrls不为空，则会重新用新的配置生成新的invoker，然后销毁原先的invoker。</p>
<p>接下来重点分析Dubbo关于override协议的解析实现细节。</p>
<h2 id="1、dubbo关于override类图"><a href="#1、dubbo关于override类图" class="headerlink" title="1、dubbo关于override类图"></a>1、dubbo关于override类图</h2><p><img src="https://img-blog.csdn.net/20180624230247836?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述"></p>
<ol>
<li>Configurator：协议配置接口，主要抽象出两个接口方法：</li>
</ol>
<ul>
<li>URL getUrl()：获取配置URL。</li>
<li>URL configure(URL url)：根据configureUrl来配置 URL url。</li>
</ul>
<ol start="2">
<li>AbstractConfigurator：协议配置抽象实现类（模板类）。</li>
<li>AbsentConfigurator：absent配置器，其策略是，如果configureUrl存在的属性，则不覆盖。</li>
<li>OverrideConfigurator：override配置器，其策略是，直接覆盖属性。</li>
</ol>
<h2 id="2、源码分析OverrideConfigurator实现原理"><a href="#2、源码分析OverrideConfigurator实现原理" class="headerlink" title="2、源码分析OverrideConfigurator实现原理"></a>2、源码分析OverrideConfigurator实现原理</h2><h3 id="2-1-源码分析AbstractConfigurator-configure"><a href="#2-1-源码分析AbstractConfigurator-configure" class="headerlink" title="2.1 源码分析AbstractConfigurator#configure"></a>2.1 源码分析AbstractConfigurator#configure</h3><p>AbstractConfigurator#configure</p>
<ol>
<li>Configurator：协议配置接口，主要抽象出两个接口方法：</li>
</ol>
<ul>
<li>URL getUrl()：获取配置URL。</li>
<li>URL configure(URL url)：根据configureUrl来配置 URL url。</li>
</ul>
<ol start="2">
<li>AbstractConfigurator：协议配置抽象实现类（模板类）。</li>
<li>AbsentConfigurator：absent配置器，其策略是，如果configureUrl存在的属性，则不覆盖。</li>
<li>OverrideConfigurator：override配置器，其策略是，直接覆盖属性。</li>
</ol>
<h3 id="2-2-源码分析AbstractConfigurator-configure"><a href="#2-2-源码分析AbstractConfigurator-configure" class="headerlink" title="2.2 源码分析AbstractConfigurator#configure"></a>2.2 源码分析AbstractConfigurator#configure</h3><p>AbstractConfigurator#configure</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">public URL configure(URL url) &#123;</span><br><span class="line">        if (configuratorUrl &#x3D;&#x3D; null || configuratorUrl.getHost() &#x3D;&#x3D; null</span><br><span class="line">                || url &#x3D;&#x3D; null || url.getHost() &#x3D;&#x3D; null) &#123;     &#x2F;&#x2F; @1</span><br><span class="line">            return url;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        if (configuratorUrl.getPort() !&#x3D; 0) &#123;          &#x2F;&#x2F;  @2</span><br><span class="line">            if (url.getPort() &#x3D;&#x3D; configuratorUrl.getPort()) &#123;</span><br><span class="line">                return configureIfMatch(url.getHost(), url);      &#x2F;&#x2F; @3</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            if (url.getParameter(Constants.SIDE_KEY, Constants.PROVIDER).equals(Constants.CONSUMER)) &#123;   &#x2F;&#x2F; @4</span><br><span class="line">                return configureIfMatch(NetUtils.getLocalHost(), url);&#x2F;&#x2F; NetUtils.getLocalHost is the ip address consumer registered to registry.</span><br><span class="line">            &#125; else if (url.getParameter(Constants.SIDE_KEY, Constants.CONSUMER).equals(Constants.PROVIDER)) &#123;   &#x2F;&#x2F; @5</span><br><span class="line">                return configureIfMatch(Constants.ANYHOST_VALUE, url);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        return url;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>代码@1：如果configuratorUrl (配置URL)为空host为空，或url为空或host为空，则返回url。这里参数的覆盖方向  configuratorUrl —-&gt; url。<br>代码@2：如果configuratorUrl如果端口不为空，则需要判断url的端口，端口必须相同，才执行configuratorUrl配置url。<br>代码@3，执行具体的配置操作，下文待分析。<br>代码@4、@5：如果端口为空，该配置URL(configuratorUrl)的类型要么是针对消费者，要么地址是0.0.0.0(任意)。</p>
<p>如果url属于服务消费者，host为消费者的注册IP地址，如果是服务提供者，则host为0.0.0.0来配置。</p>
<h3 id="2-3-源码分析AbstractConfigurator-configureIfMatch"><a href="#2-3-源码分析AbstractConfigurator-configureIfMatch" class="headerlink" title="2.3 源码分析AbstractConfigurator#configureIfMatch"></a>2.3 源码分析AbstractConfigurator#configureIfMatch</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">private URL configureIfMatch(String host, URL url) &#123;</span><br><span class="line">        if (Constants.ANYHOST_VALUE.equals(configuratorUrl.getHost()) || host.equals(configuratorUrl.getHost())) &#123;</span><br><span class="line">            String configApplication &#x3D; configuratorUrl.getParameter(Constants.APPLICATION_KEY,</span><br><span class="line">                    configuratorUrl.getUsername());</span><br><span class="line">            String currentApplication &#x3D; url.getParameter(Constants.APPLICATION_KEY, url.getUsername());</span><br><span class="line">            if (configApplication &#x3D;&#x3D; null || Constants.ANY_VALUE.equals(configApplication)</span><br><span class="line">                    || configApplication.equals(currentApplication)) &#123;</span><br><span class="line">                Set&lt;String&gt; condtionKeys &#x3D; new HashSet&lt;String&gt;();</span><br><span class="line">                condtionKeys.add(Constants.CATEGORY_KEY);</span><br><span class="line">                condtionKeys.add(Constants.CHECK_KEY);</span><br><span class="line">                condtionKeys.add(Constants.DYNAMIC_KEY);</span><br><span class="line">                condtionKeys.add(Constants.ENABLED_KEY);</span><br><span class="line">                for (Map.Entry&lt;String, String&gt; entry : configuratorUrl.getParameters().entrySet()) &#123;</span><br><span class="line">                    String key &#x3D; entry.getKey();</span><br><span class="line">                    String value &#x3D; entry.getValue();</span><br><span class="line">                    if (key.startsWith(&quot;~&quot;) || Constants.APPLICATION_KEY.equals(key) || Constants.SIDE_KEY.equals(key)) &#123;</span><br><span class="line">                        condtionKeys.add(key);</span><br><span class="line">                        if (value !&#x3D; null &amp;&amp; !Constants.ANY_VALUE.equals(value)</span><br><span class="line">                                &amp;&amp; !value.equals(url.getParameter(key.startsWith(&quot;~&quot;) ? key.substring(1) : key))) &#123;</span><br><span class="line">                            return url;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">                return doConfigure(url, configuratorUrl.removeParameters(condtionKeys));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        return url;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>该方法主要实现的功能就是排除不能动态修改的属性，不支持属性主要包括：category、check、dynamic、enabled、还有以<del>开头的属性，并且如果</del>开头的属性，配置URL与原URL的值不相同，则不使用该配置URL重写原URL。将配置URL(configuratorUrl)移除不支持属性后，调用其子类的doConfigure方法覆盖属性，Dubbo默认支持如下覆盖策略</p>
<ul>
<li>override 直接覆盖。</li>
<li>absent，如果原先存在该属性的配置，则以原先配置的属性值优先，如果原先没有配置该属性，则添加新的配置属性。</li>
</ul>
<p>总结一下：当在dubbo-admin(管理后台)中创建一条override规则后，会首先存储在注册中心（zookeeper的指定目录下${service}/configurators目录下，此时基于注册中心的事件机制，会通知相关监听者（服务消费者），服务消费者收到最新的配置时，会根据最新的配置重新构建Invoker对象，然后销毁原先的Invoker对象。</p>
</div>

			<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
			<script>
			var isMobile = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);
			if (!isMobile) {
			    var btw = new BTWPlugin();
			    btw.init({
			        "id": "vip-container",
			        "blogId": "18019-1573088808868-542",
			        "name": "中间件兴趣圈",
			        "qrcode": "https://img-blog.csdnimg.cn/20190314214003962.jpg",
			        "keyword": "more"
			    });
			}
			</script>
		
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/posts/497923c7.html" rel="next" title="全链路压测必备基础组件之线程上下文管理之“三剑客”">
                <i class="fa fa-chevron-left"></i> 全链路压测必备基础组件之线程上下文管理之“三剑客”
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/posts/b5542131.html" rel="prev" title="源码分析Dubbo集群容错策略">
                源码分析Dubbo集群容错策略 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">139</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">18</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1%E3%80%81dubbo%E5%85%B3%E4%BA%8Eoverride%E7%B1%BB%E5%9B%BE"><span class="nav-number">1.</span> <span class="nav-text">1、dubbo关于override类图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2%E3%80%81%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90OverrideConfigurator%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86"><span class="nav-number">2.</span> <span class="nav-text">2、源码分析OverrideConfigurator实现原理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90AbstractConfigurator-configure"><span class="nav-number">2.1.</span> <span class="nav-text">2.1 源码分析AbstractConfigurator#configure</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90AbstractConfigurator-configure"><span class="nav-number">2.2.</span> <span class="nav-text">2.2 源码分析AbstractConfigurator#configure</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90AbstractConfigurator-configureIfMatch"><span class="nav-number">2.3.</span> <span class="nav-text">2.3 源码分析AbstractConfigurator#configureIfMatch</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">中间件兴趣圈</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("NNEhOL0iOcflg8f1U3HUqiCq-gzGzoHsz", "7kSmkbbb3DktmHALlShDsBUF");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  

  

  

  

</body>
</html>
